package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.mapper.CloudFileMapper;
import com.campus.commons.entity.CheckEvening;
import com.campus.app.mapper.CheckEveningMapper;
import com.campus.app.service.CheckEveningsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.MyPage;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;

/**
 * <p>
 * 晚自习值班表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class CheckEveningServiceImpl extends ServiceImpl<CheckEveningMapper, CheckEvening> implements CheckEveningsService {
    @Autowired
    private CheckEveningMapper checkEveningMapper;

    @Autowired
    private CloudFileMapper fileMapper;

    @Override
    public Result<List<CheckEvening>> getAllCheckEvening(MyPage<CheckEvening> myPage) {
        return Result.result(checkEveningMapper.selectPage(new Page<CheckEvening>(myPage.getPage(),myPage.getLimit()),null));
    }

    @Override
    public Result<List<CheckEvening>> getCheckEvenings(MyPage<CheckEvening> myPage, Date start, Date end) {
        QueryWrapper<CheckEvening> queryWrapper = new QueryWrapper<>();
        String starts = Tool.getDateToString(start);
        String ends = Tool.getDateToString(end);
        CheckEvening data = myPage.getData();
        queryWrapper.inSql("classId","select c.id from class c where c.className like '%"+data.getClassName()+"%'");
        queryWrapper.between(start!=null,"Date(date)", starts, ends);
        queryWrapper.eq(data.getIsred()!=null,"isRed", data.getIsred());
        return Result.result(checkEveningMapper.selectPage(new Page<CheckEvening>(myPage.getPage(),myPage.getLimit()),queryWrapper));

    }

    @Override
    public Result addCheckEvening(CheckEvening checkEvening) {
        return Result.result(checkEveningMapper.insert(checkEvening));
    }

    @Override
    public Result delCheckEvening( Integer id) {
        return Result.result(checkEveningMapper.deleteById(id));
    }

    @Override
    public Result upCheckEvening(CheckEvening checkEvening) {
        return Result.result(checkEveningMapper.updateById(checkEvening));
    }
    @Async
    @Override
    public void exportExcelForCheckEvening() {
        Collection<CheckEvening> values = checkEveningMap.values();
        if (values.size()<1) {
            throw new RuntimeException("数据有误，无法执行导出表格");
        }
        HashMap<String, Object> data = new HashMap<>();
        data.put("date", Tool.getDateToString(null));
        data.put("", values);
        checkEveningMap.clear();//清空；
        fileMapper.addFile(Tool.exportExcel("晚自习值班模板.xlsx",  "晚自习值班表.xlsx",data,1));
    }

    @Override
    public Result addCheckEveningMap(CheckEvening checkEvening)
    {
        if (StringUtils.isEmpty(checkEvening.getClassName())) {
            return Result.error(Error.Map_E3R, null);
        }
        checkEveningMap.put(checkEvening.getClassid(), checkEvening);
        return Result.result(1);

    }

    @Override
    public Result upCheckEventingMap(CheckEvening checkEvening) {
        addCheckEveningMap(checkEvening);
        return Result.result(1);
    }

    @Override
    public Result getCacheMap() {
        return Result.success(checkEveningMap);
    }

    @Override
    public CheckEvening getOne(int id) {
        return checkEveningMapper.selectById(id);
    }

    @Override
    public Result delCheckEventingMap(Integer classId) {
       checkEveningMap.remove(classId);
        return Result.result(1);
    }


}
